#ifndef _STAGE_H_
#define _STAGE_H_

#include "image.hpp"

#include <string>
#include <map>

#include "grid.hpp"
#include "vec.hpp"

class Stage
{
public:
	Stage();
	Stage(int c, int r);
	~Stage();

	void Init(int c, int r);
	bool Open(std::string & name);
	bool Save(std::string & name);
	void Close();

	void Draw();
	void PathDraw();

	void Clamp(int & col, int & row);

	bool PropGet(int tile, int prop);
	void PropSet(int tile, int prop);
	void PropUnset(int tile, int prop);
	int PropsFind(int c1, int r1, int c2, int r2);

	float PropArea(int prop, const Box2f & b)
	{
		return PropArea(prop, b.AxMin(X), b.AxMin(Y), 
				b.AxMax(X), b.AxMax(Y));
	}
	float PropArea(int prop, float x1, float y1, float x2, float y2);

	int PropBits(int tile);

	float AxisHit(const Box2f & b, int ax)
	{
		return AxisHit(b, ax, PropBit(SOLID));
	}
	float AxisHit(const Box2f & b, int ax, int hitprops);
	Vec2f BoxHitVec(int c, int r, const Box2f & b);

	int Get(int col, int row)
	{
		return grid.Get(col, row);
	}

	int PathGet(int col, int row)
	{
		return pathgrid.Get(col, row);
	}

	Vec2f PathNextStep(int sc, int sr, int fc, int fr);

	void Put(int col, int row, int num)
	{
		grid.Put(col, row, num);
	}

	int Replace(int c, int r, const Grid & repl);

	enum Prop
	{
		NOPROP = -1,
		SOLID,
		URSTAIR, 
		DRSTAIR,
		WATER,
		FLAMM,
		PROPCT
	};

	int PropBit(int prop)
	{
		return 1 << prop;
	}

	int Cols()
	{
		return grid.Cols();
	}

	int Rows()
	{
		return grid.Rows();
	}

	void Resize(int cols, int rows)
	{
		grid.Resize(cols, rows);
	}

	Image* TileImg()
	{
		return tileimg;
	}

private:
	Image *tileimg;
	std::map<int, int> tprops;
	Grid grid;
	Grid pathgrid;
	Grid pathscores;
};

extern Stage Stg;
#endif

